$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'water/tdeviceinfo/list',
        datatype: "json",
        colModel: [
            { label: '内部名称', name: 'neiName', index: 'nei_name', width: 80 },
			{ label: '设备号', name: 'deviceId', index: 'device_id', width: 100, key: true },
			{ label: '主机通道', name: 'masterNum', index: 'master_num', width: 80 }, 			
			{ label: '设备二管号', name: 'slaveNum', index: 'slave_num', width: 80 }, 			
			{ label: '采集卡号', name: 'cardNum', index: 'card_num', width: 80 }, 			
			{ label: '设备区域', name: 'deviceArea', index: 'device_area', width: 80 }, 			
			{ label: '设备小区', name: 'deviceNei', index: 'device_nei', width: 80 }, 			
			{ label: '设备楼号', name: 'deviceBuild', index: 'device_build', width: 80 }, 			
			{ label: '设备单元', name: 'deviceUnit', index: 'device_unit', width: 80 }, 			
			{ label: '采集通道', name: 'routeNum', index: 'route_num', width: 80 }, 			
			{ label: '表号', name: 'meterNum', index: 'meter_num', width: 80 },
            { label: '设备类型', name: 'typeName', index: 'type_id', width: 120 },
			{ label: '类型码', name: 'typeId', index: 'type_id', width: 80,hidden:true },
			{ label: '备注1', name: 'memo1', index: 'memo1', width: 80 ,hidden:true},
			{ label: '备注2', name: 'memo2', index: 'memo2', width: 80 ,hidden:true},
			{ label: '备注3', name: 'memo3', index: 'memo3', width: 80 ,hidden:true}
        ],
		viewrecords: true,
        height: $(window).height() > 385 ?  $(window).height() - 150 : 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 65,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            jQuery("#jqGrid").jqGrid('setLabel', 'rn', '序号', {
                'text-align': 'center',
                'vertical-align': 'middle',
                //"width": "50"
            });

            $(window).on('resize', function() {
                var $content = $('.content');
                $content.height($(this).height() - 154);
                $content.find('iframe').each(function() {
                    $(this).height($content.height());
                });

                //$(window.parent.document).find("#if1").height($("#content").height());

                $("#deptTreeLeft").css({height:($("#content > div.col-md-10").height()-45)+'px'});
            }).resize();
        }
    });
    $(function () {
        $('#inputClear').click(function () {
            $('input')[0].value = "";
            vm.q.value="";
        })
    });
});



function zTreeOnDblClick(event, treeId, treeNode) {
    vm.q.dept = treeNode.deptId;
    vm.newpage = true;
    if(!treeNode.isParent) {
        var x = treeNode.deptId.split("_");
        if (x.length == 4){
            vm.q.dept = x[3];
            vm.reload();
        }
    }else{
        vm.q.dept='';
    }
    //alert(treeNode.deptId);
    //alert(treeNode ? treeNode.tId + ", " + treeNode.name : "isRoot");
};

var settingLeft = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onDblClick: zTreeOnDblClick
    }
};

var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tDeviceInfo: {},
        newpage : false,
        searchKeyDictInfo: {},
        q:{
            key:null,
            value:null,
            dept:null
        }
	},
	methods: {
        getSearchKeyDict: function(){
            $.get(baseURL + "sys/dict/list",{name:"qkey_tdeviceinfo"}, function(r){
                vm.searchKeyDictInfo = r.page.list;
            });
        },
        query: function () {
            if(vm.q.value != null && vm.q.value != ""
                && (vm.q.key == null || vm.q.key == "")){
                alert('请选择查询条件!');
                return;
            }
            vm.newpage=true;
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tDeviceInfo = {};
		},
		update: function (event) {
			var deviceId = getSelectedRow();
			if(deviceId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(deviceId)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.tDeviceInfo.deviceId == null ? "water/tdeviceinfo/save" : "water/tdeviceinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tDeviceInfo),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var deviceIds = getSelectedRows();
			if(deviceIds == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "water/tdeviceinfo/delete",
                        contentType: "application/json",
                        data: JSON.stringify(deviceIds),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(deviceId){
			$.get(baseURL + "water/tdeviceinfo/info/"+deviceId, function(r){
                vm.tDeviceInfo = r.tDeviceInfo;
            });
		},
        getDeptTreeLeft: function(){
            $.get(baseURL + "water/waterdept/queryDeptAll", function(r){
                //console.log(r);
                ztree = $.fn.zTree.init($("#deptTreeLeft"), settingLeft, r);
                var node = ztree.getNodeByParam("deptId", vm.q.dept);
                if(node != null){
                    ztree.selectNode(node);
                    vm.q.dept = node.deptId;
                }
            })
        },
        outxls: function(){
            var total = $("#jqGrid").jqGrid('getGridParam', 'records') + "";
            var dept = vm.q.dept==null ? "" : vm.q.dept+  "";
            var url = baseURL
                + "water/tdeviceinfo/export?params="
                + encodeURIComponent(JSON.stringify({key:vm.q.key,value:vm.q.value,dept:dept,limit:total,page:"1"}));

            //console.log(decodeURIComponent(url));
            location.href = url;
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            if(vm.newpage){page = "1";}
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'key': vm.q.key,'value':vm.q.value,'dept':vm.q.dept},
                page:page
            }).trigger("reloadGrid");
        }
    },
    created: function (){
        this.getDeptTreeLeft();
        this.getSearchKeyDict();
    }
});